﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data;
using System.Data.Common;
using System.Data.SQLite;

namespace projectOklahoma
{
    public struct connType
    {
        public string dbname;
        public string username;
        public string password;
        public string lastConnected;
        public int connId;
    }

    public partial class ConnectionForm : Form
    {
        private static string dbpath = "d:\\prova\\connections.db";
        private static string connString = "Data Source=" + dbpath + ";Version=3";

        private bool inited = false;
        private System.Collections.ArrayList connList;

        public ConnectionForm()
        {
            InitializeComponent();
            connList = new System.Collections.ArrayList();
        }

        private int getConnId(string dbname, string user,ref string pwd)
        {
            SQLiteConnection con = new SQLiteConnection(connString);
            SQLiteCommand cmd = con.CreateCommand();

            cmd.CommandText = "select connid,password from connections where " +
                "dbname='" + dbname + "' and " +
                "username='" + user + "'";

            con.Open();
            SQLiteDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                int cnid = reader.GetInt32(0);
                pwd = reader.GetString(1);
                con.Close();
                cmd.Dispose();
                return cnid;
            }

            con.Close();
            cmd.Dispose();

            return -1;
        }

        private void ConnectionForm_Load(object sender, EventArgs e)
        {
            SQLiteConnection con = new SQLiteConnection(connString);
            SQLiteCommand cmd = con.CreateCommand();

            cmd.CommandText = "select connid,dbname,lastconnected,username,password from connections";

            con.Open();
            SQLiteDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                connType cn = new connType();
                cn.connId = reader.GetInt32(0);
                cn.dbname = reader.GetString(1);
                cn.lastConnected = reader.GetString(2);
                cn.username = reader.GetString(3);
                cn.password= reader.GetString(4);

                int n = dgridConns.Rows.Add();
                dgridConns.Rows[n].Cells[0].Value = reader.GetString(1);
                dgridConns.Rows[n].Cells[1].Value = reader.GetString(3);
                dgridConns.Rows[n].Cells[2].Value = "*******";
                dgridConns.Rows[n].Cells[3].Value = reader.GetString(2);
            }

            con.Close();
            cmd.Dispose();

            inited = true;
        }

        private void dgridConns_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (inited)
            {
                datasourceCombo.Text = dgridConns.Rows[e.RowIndex].Cells[0].Value.ToString();
                usernameTextbox.Text = dgridConns.Rows[e.RowIndex].Cells[1].Value.ToString();

                string pwd = "";
                getConnId(datasourceCombo.Text, usernameTextbox.Text, ref pwd);
                passwordTextbox.Text = pwd;
            }
        }

        private void dgridConns_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // double click on a cell (db connection)
            // try to connect.

            string username= dgridConns.Rows[e.RowIndex].Cells[1].Value.ToString();
            string dbname = dgridConns.Rows[e.RowIndex].Cells[0].Value.ToString();
            string pwd="";

            int cnid = getConnId(dbname, username,ref pwd);

            string errstring = "";

            int konnum = OracleConnectionManager.newConnection(username, pwd, dbname, ref errstring);
            if (konnum<0)
            {
                System.Windows.Forms.MessageBox.Show(errstring);
                return;
            }

            workingForm wf = new workingForm(konnum);
            wf.MdiParent = this.MdiParent;
            wf.Show();

            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // connection useing new/existing parameters

            string dscombo = datasourceCombo.Text;
            string usrtxt = usernameTextbox.Text;
            string pwd = "";

            int cnid = getConnId(dscombo, usrtxt, ref pwd);
            if (cnid == -1)
            {
                // try new connection, and, if successful, store parameters
            }
            else
            {
                // connect normally
            }
        }
    }
}
